From 9104f1857f6e732e5e90c296b1575bbe72923ff7 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 11 May 2012 18:58:55 +0100 Subject: [PATCH] libxl: handle POLLERR, POLLHUP, POLLNVAL properly Pass POLLERR and POLLHUP to fd callbacks, as is necessary. Crash on POLLNVAL since that means our fds are messed up. Document the behaviour (including the fact that poll sometimes sets POLLHUP or POLLERR even if only POLLIN was requested. Fix the one current fd callback to do something with POLLERR|POLLHUP. Signed-off-by: Ian Jackson Acked-by: Ian Campbell Committed-by: Ian Jackson --- tools/libxl/libxl_event.c | 7 ++++++- tools/libxl/libxl_internal.h | 5 +++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/tools/libxl/libxl_event.c b/tools/libxl/libxl_event.c index e11a4e7603..5046938fd8 100644 --- a/tools/libxl/libxl_event.c +++ b/tools/libxl/libxl_event.c @@ -339,6 +339,9 @@ static void watchfd_callback(libxl__egc *egc, libxl__ev_fd *ev, { EGC_GC; + if (revents & (POLLERR|POLLHUP)) + LIBXL__EVENT_DISASTER(egc, "unexpected poll event on watch fd", 0, 0); + for (;;) { char **event = xs_check_watch(CTX->xsh); if (!event) { @@ -743,7 +746,9 @@ static int afterpoll_check_fd(libxl__poller *poller, /* again, stale slot entry */ return 0; - int revents = fds[slot].revents & events; + assert(!(fds[slot].revents & POLLNVAL)); + + int revents = fds[slot].revents & (events | POLLERR | POLLHUP); /* we mask in case requested events have changed */ return revents; diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 8947ddd6fc..74e640f747 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -128,6 +128,11 @@ _hidden void libxl__alloc_failed(libxl_ctx *, const char *func, typedef struct libxl__ev_fd libxl__ev_fd; typedef void libxl__ev_fd_callback(libxl__egc *egc, libxl__ev_fd *ev, int fd, short events, short revents); + /* Note that revents may contain POLLERR or POLLHUP regardless of + * events; otherwise revents contains only bits in events. Contrary + * to the documentation for poll(2), POLLERR and POLLHUP can occur + * even if only POLLIN was set in events. (POLLNVAL is a fatal + * error and will cause libxl event machinery to fail an assertion.) */ struct libxl__ev_fd { /* caller should include this in their own struct */ /* read-only for caller, who may read only when registered: */ -- 2.30.2